/**
 * 
 */
package pvt.delez.pa.depend;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

import pam.Project;


/**
 * @author Administrator
 * 
 */
public class AnalysisDependAction implements IObjectActionDelegate {
	private ISelection selection;
	private static final String EDITOR_ID = "pvt.delez.pam.gef.editors.PAMEditor";

	// "pam.presentation.PamEditorID";;

	@Override
	public void run(IAction action) {
		if (!(selection instanceof IStructuredSelection)) {
			return;
		}
		AnalysisProjectDepend projectAnalysis = new AnalysisProjectDepend();
		AnalysisLibraryDepend libraryDepend = new AnalysisLibraryDepend();

		for (Iterator<?> it = ((IStructuredSelection) selection).iterator(); it
				.hasNext();) {
			try {
				Object element = it.next();
				IJavaProject javaProject = null;
				if (element instanceof IJavaProject) {
					javaProject = (IJavaProject) element;
				} else if (element instanceof IAdaptable) {
					javaProject = (IJavaProject) ((IAdaptable) element)
							.getAdapter(IJavaProject.class);
				} else if (element instanceof IProject
						&& ((IProject) element)
								.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
					javaProject = JavaCore.create((IProject) element);
				}

				if (javaProject != null) {
					Project p = projectAnalysis.analysis(javaProject);
					projectAnalysis.addPrimaryProject(p);

					libraryDepend.analysis(javaProject);
					
				}
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		Set<Project> primaryProjects = projectAnalysis.getPrimaryProjects();
		//PAM.setProjects(primaryProjects, projectAnalysis.getAllProjects());

		ProjectInput input = new ProjectInput(primaryProjects);
		IWorkbenchPage page = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage();
		// For finding a specific Editor, an instance of EditorInput is
		// required.
		try {
			// IDE.getEditorDescriptor(p.getName() + ".pam").getId();
			IDE.openEditor(page, input, EDITOR_ID);
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;
	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart part) {

	}

	private static class ProjectInput implements IEditorInput {
		private final Set<Project> projects;

		public ProjectInput(Set<Project> projects) {
			this.projects = projects;
		}

		@Override
		public Object getAdapter(Class arg0) {
			if (arg0 == Project.class) {
				return projects;
			} else if (arg0 == String.class) {
				return getName();
			} else if (arg0 == Set.class) {
				return projects;
			}
			return null;
		}

		@Override
		public boolean exists() {
			return false;
		}

		@Override
		public ImageDescriptor getImageDescriptor() {
			return null;
		}

		@Override
		public String getName() {
			String names = getNames().toString();
			return names.length() > 16 ? names.substring(16) + "..." : names;
		}

		@Override
		public IPersistableElement getPersistable() {
			return new IPersistableElement() {

				@Override
				public void saveState(IMemento arg0) {

				}

				@Override
				public String getFactoryId() {
					return "AnalysisDepend";
				}

			};
		}

		@Override
		public String getToolTipText() {

			return getNames().toString();
		}

		private Set<String> getNames() {
			Set<String> names = new HashSet<String>();
			for (Project p : projects) {
				names.add(p.getName());
			}
			return names;
		}
	}
}
